/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.MemberMoney;
import com.boxin.ims.modules.wechat.dao.MemberMoneyDao;

/**
 * 会员金额Service
 * @author zhangshanshan
 * @version 2014-12-18
 */
@Component
@Transactional(readOnly = true)
public class MemberMoneyService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MemberMoneyService.class);
	
	@Autowired
	private MemberMoneyDao memberMoneyDao;
	
	@Autowired
	private QRCodeService qrCodeService;
	
	public MemberMoney get(Long id) {
		return memberMoneyDao.findOne(id);
	}
	
	public Page<MemberMoney> find(Page<MemberMoney> page, MemberMoney memberMoney) {
		DetachedCriteria dc = memberMoneyDao.createDetachedCriteria();
		if (memberMoney.getUser()!=null && memberMoney.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", memberMoney.getUser().getId()));
		}
		dc.addOrder(Order.desc("id"));
		return memberMoneyDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(MemberMoney memberMoney) {
		
		if("3".equals(memberMoney.getType())){
			
			int width = QRCodeUtils.WIDTH;
			int height =QRCodeUtils.HEIGHT;
			//生成二维码
			QRCode qrcode = new QRCode();
			String content = QRCodeUtils.generatePayMoneySN(memberMoney.getUser().getId());
			qrcode.setContent(content);
			String path = Constants.getWechatResourceSavePath();
			path += DateUtils.getTodayString()+File.separator+content;
			FileUtils.createDirectory(path);
			QRCodeUtils.generateQRCodePNG(content, path, width,height);
			qrcode.setFileName(content+".png");
			qrcode.setPngPath(path+".png");
			
			qrcode.setUser(memberMoney.getUser());
			qrcode.setMember(memberMoney.getWechatMember());
			qrcode.setType(QRCode.TYPE_PAY_MONEY);
			qrcode.setName("支付");
			
			qrCodeService.save(qrcode);	//保存二维码		
			
			memberMoney.setQrCode(qrcode);
		}
		
		
		memberMoney.setDay(DateUtils.getTodayString());
		memberMoney.setMonth(com.thinkgem.jeesite.common.utils.DateUtils.getMonth());
		memberMoney.setYear(DateUtils.getTodayString().substring(0,4));
		
		
		memberMoneyDao.save(memberMoney);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		memberMoneyDao.deleteById(id);
	}
	
}
